﻿using UnityEditor;
using UnityEngine;

namespace E.Story
{
    /// <summary>
    /// 输入输出实用类
    /// </summary>
    public static class IOUtility
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="folderName">文件夹名称</param>
        public static void CreateFolder(string path, string folderName)
        {
            // 检测目标文件夹是否存在
            if (AssetDatabase.IsValidFolder($"{path}/{folderName}"))
            {
                return;
            }

            AssetDatabase.CreateFolder(path, folderName);
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="fullPath">路径</param>
        public static void RemoveFolder(string fullPath)
        {
            FileUtil.DeleteFileOrDirectory($"{fullPath}.meta");
            FileUtil.DeleteFileOrDirectory($"{fullPath}/");
        }

        /// <summary>
        /// 创建资产文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="path">路径</param>
        /// <param name="assetName">资产名称</param>
        /// <returns>资产文件</returns>
        public static T CreateAsset<T>(string path, string assetName) where T : ScriptableObject
        {
            string fullPath = $"{path}/{assetName}.asset";

            // 尝试加载目标文件
            T asset = LoadAsset<T>(path, assetName);

            // 若文件不存在，则创建一个
            if (asset == null)
            {
                asset = ScriptableObject.CreateInstance<T>();
                AssetDatabase.CreateAsset(asset, fullPath);
            }

            return asset;
        }

        /// <summary>
        /// 载入资产文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="path">路径</param>
        /// <param name="assetName">文件名</param>
        /// <returns>资产文件</returns>
        public static T LoadAsset<T>(string path, string assetName) where T : ScriptableObject
        {
            string fullPath = $"{path}/{assetName}.asset";
            return AssetDatabase.LoadAssetAtPath<T>(fullPath);
        }

        /// <summary>
        /// 载入资产文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="fullPath">路径</param>
        /// <returns>资产文件</returns>
        public static T LoadAsset<T>(string fullPath) where T : ScriptableObject
        {
            return AssetDatabase.LoadAssetAtPath<T>(fullPath);
        }

        /// <summary>
        /// 删除资产文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="assetName">文件名</param>
        public static void RemoveAsset(string path, string assetName)
        {
            AssetDatabase.DeleteAsset($"{path}/{assetName}.asset");
        }

        /// <summary>
        /// 将资产文件写入硬盘
        /// </summary>
        /// <param name="asset">资产文件</param>
        public static void SaveAsset(Object asset)
        {
            EditorUtility.SetDirty(asset);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}